package Midium;
//36.有效的数独
import java.util.HashMap;
import java.util.Map;

public class Solution36 {
    public boolean isValidSudoku(char[][] board) {
        //先判断行
        for (int i = 0; i < board.length; i++) {
            Map map = new HashMap();
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] != '.' && map.containsKey(board[i][j]))
                    return false;
                else
                    map.put(board[i][j], 1);
            }
        }
        //判断列
        for (int i = 0, j = 0; j < 9; j++) {
            Map map = new HashMap();
            for (i = 0; i < 9; i++) {
                if (board[i][j] != '.' && map.containsKey(board[i][j]))
                    return false;
                else
                    map.put(board[i][j], 1);
            }
        }
        //判断九宫格
        for (int u = 0; u < 3; u++) {
            for (int v = 0; v < 3; v++) {
                char[][] nc = new char[3][3];
                for (int k = 0, i = 3 * u; k < 3; k++, i++) {
                    for (int m = 0, j = 3 * v; m < 3; m++, j++) {
                        nc[k][m] = board[i][j];
                    }
                }
                if(!isValid(nc))
                    return false;
            }
        }
        return true;
    }

    //九宫格
    public boolean isValid(char[][] board) {
        Map map = new HashMap();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] != '.' && map.containsKey(board[i][j]))
                    return false;
                else
                    map.put(board[i][j], 1);
            }
        }
        return true;
    }
}
